<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

class Hd_invitation_lottery_time extends PC_controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('Usermodel','m_user');
        $this->load->model('Hdinvitationlotterytimemodel','m_lottery_time');
        $this->load->model('configmodel','m_config');
    }

    public function index(){
        $data = parent::index();
        global $g_hd_invitation_gift_type;

        $data['ctrlName'] = $this->router->fetch_class();
        $data['game_deadline'] = $this->m_config->getConfig(HD_INVITATION_DEADLINE);
       	$data['game_gift'] =  array4js($g_hd_invitation_gift_type);

        $this->load->view('admin/hd_invitation_lottery_time',$data);
    }

    public function getLotteryTimeList(){
        $assist['where'] = getFilter($this->input->post(),Hdinvitationlotterytimemodel::$searchField);
        $assist['like'] = getFilter($this->input->post(),Hdinvitationlotterytimemodel::$likeField);
        $assist['sort'] = getSort($this->input->post(),Hdinvitationlotterytimemodel::$sortField);
        $assist['pagination'] = getPagination($this->input->post());

        list($list,$count) = $this->m_lottery_time->getLotteryTimeListSection($assist);

        $this->response->setSuccess();
        $this->response->setItems($list);
        $this->response->setTotalCount($count);
        $this->response->responseExit();
    }

    public function createLotteryTime() {
        $this->validate->setRule('lottery_time_date','string');
        $this->validate->setRule('lottery_time_date','message',$this->lang->line('error_parameter'));
        $this->validate->setRule('lottery_time_start','string');
        $this->validate->setRule('lottery_time_start','message',$this->lang->line('error_parameter'));
        $this->validate->setRule('lottery_time_end','string');
        $this->validate->setRule('lottery_time_end','message',$this->lang->line('error_parameter'));
        for($i=1;$i<=9;$i++) {
            $this->validate->setRule('lottery_time_gift'.$i.'_rate','float');
            $this->validate->setRule('lottery_time_gift'.$i.'_count','integer');
        }
        if(!$this->validate->run($this->input->post())){
            $this->response->setDetail($this->validate->getMessage());
            $this->response->responseExit();
        }
        $info = $this->validate->getResult();

        $info['user_id'] = $this->m_app->getCurrentUserId();
        $info['lottery_time_status'] = STATUS_ACTIVE;
        $info['lottery_time_time_create'] = NOW;
        $this->m_lottery_time->createLotteryTime($info);

        $this->response->setSuccess();
        $this->response->setDetail($this->lang->line('success_update'));
        $this->response->responseExit();
    }

    public function editLotteryTime() {
        $this->validate->setRule('lottery_time_id','integer');
        $this->validate->setRule('lottery_time_id','message',$this->lang->line('error_parameter'));
        $this->validate->setRule('lottery_time_date','string');
        $this->validate->setRule('lottery_time_date','message',$this->lang->line('error_parameter'));
        $this->validate->setRule('lottery_time_start','string');
        $this->validate->setRule('lottery_time_start','message',$this->lang->line('error_parameter'));
        $this->validate->setRule('lottery_time_end','string');
        $this->validate->setRule('lottery_time_end','message',$this->lang->line('error_parameter'));
        for($i=1;$i<=9;$i++) {
            $this->validate->setRule('lottery_time_gift'.$i.'_rate','float');
            $this->validate->setRule('lottery_time_gift'.$i.'_count','integer');
        }
        if(!$this->validate->run($this->input->post())){
            $this->response->setDetail($this->validate->getMessage());
            $this->response->responseExit();
        }
        $info = $this->validate->getResult();

        $info['user_id'] = $this->m_app->getCurrentUserId();
        $this->m_lottery_time->updateLotteryTime($info);

        $this->response->setSuccess();
        $this->response->setDetail($this->lang->line('success_update'));
        $this->response->responseExit();
    }

    public function setGameDeadline() {
        $this->validate->setRule(HD_INVITATION_DEADLINE,'string');
        $this->validate->setRule(HD_INVITATION_DEADLINE,'message',$this->lang->line('error_parameter'));
        if(!$this->validate->run($this->input->post())){
            $this->response->setDetail($this->validate->getMessage());
            $this->response->responseExit();
        }
        $info = $this->validate->getResult();

        $info['user_id'] = $this->m_app->getCurrentUserId();
        $this->m_config->editConfig(array(
            'user_id' => $this->m_app->getCurrentUserId(),
            'config_name' => HD_INVITATION_DEADLINE,
            'config_value' => $info[HD_INVITATION_DEADLINE],
        ));

        $this->response->setSuccess();
        $this->response->setDetail($this->lang->line('success_update'));
        $this->response->responseExit();
    }

    public function deleteLotteryTime() {
        $this->validate->setRule('lottery_time_id','integer');
        $this->validate->setRule('lottery_time_id','message',$this->lang->line('error_parameter'));
        if(!$this->validate->run($this->input->post())){
            $this->response->setDetail($this->validate->getMessage());
            $this->response->responseExit();
        }
        $info = $this->validate->getResult();

        $info['lottery_time_status'] = STATUS_DELETE;
        $info['user_id'] = $this->m_app->getCurrentUserId();
        $this->m_lottery_time->updateLotteryTime($info);

        $this->response->setSuccess();
        $this->response->setDetail($this->lang->line('success_update'));
        $this->response->responseExit();
    }
}
/* End of file feedback.php */
/* Location: ./application/controllers/feedback.php */
